<?php
require_once ('../../inc/functions.php');

class Orders
{
	static function GetAll()
	{
		$conn = GetConnection();	
		return $conn->query('SELECT * FROM 2012Grad_Orders');
		//$row = $response->fetch_assoc();
		//print_r($row);
	}
	
	static function Get($id)
	{
		$conn = GetConnection();	
		$results = $conn->query("SELECT * FROM 2012Grad_Orders WHERE id=$id");
		$row = $results->fetch_assoc();
		$conn->close();
		return $row;
	}
	
	static function Blank()
	{
		return array('created_at'=>null,'updated_at'=>null,'Total'=>null,'Tax'=>null,'Shipping'=>null,'ShipName'=>null,'ShipAddress'=>null,'ShipAddress2'=>null,'ShipCity'=>null,'ShipState'=>null,'ShipZip'=>null,
		'ShipPhone'=>null,'BillName'=>null,'BillAddress'=>null,'BillAddress2'=>null,'BillCity'=>null,'BillState'=>null,'BillZip'=>null,'BillPhone'=>null,'Date'=>null,'PaymentMethod'=>null,'CardNumber'=>null,
		'Shipped'=>null,'TrackingNumber'=>null,'User_id'=>null);
	}
	
	static function Insert($row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
        $sql =  "INSERT INTO 2012Grad_Orders "
        		. "(created_at, updated_at, Total, Tax, Shipping, ShipName, ShipAddress, ShipAddress2, ShipCity, ShipState, ShipZip, ShipPhone, "
        		. "BillName, BillAddress, BillAddress2, BillCity, BillState, BillZip, BillPhone, Date, PaymentMethod, CardNumber, Shipped, TrackingNumber, User_id) "
                . "VALUES (Now(), Now(), '$row2[Total]', '$row2[Tax]', '$row2[Shipping]', '$row2[ShipName]', '$row2[ShipAddress]', '$row2[ShipAddress2]', '$row2[ShipCity]', "
                . "'$row2[ShipState]', '$row2[ShipZip]', '$row2[ShipPhone]', '$row2[BillName]', '$row2[BillAddress]', '$row2[BillAddress2]', '$row2[BillCity]', '$row2[BillState]', '$row2[BillZip]', "
                . "'$row2[BillPhone]', Now(), '$row2[PaymentMethod]', '$row2[CardNumber]', '$row2[Shipped]', '$row2[TrackingNumber]', '$row2[User_id]') ";
        //echo $sql;
        $conn->query($sql);
        $error = $conn->error;
        $conn->close();
                
        return $error != '' ? array('Server Error' => $error) : true;           
	}
	
	static function Update($row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
        $sql =  "UPDATE 2012Grad_Orders "
                . "Set created_at='$row2[created_at]',updated_at=Now(),Total='$row2[Total]',Tax='$row2[Tax]',Shipping='$row2[Shipping]',ShipName='$row2[ShipName]', "
                . "ShipAddress='$row2[ShipAddress]',ShipAddress2='$row2[ShipAddress2]',ShipCity='$row2[ShipCity]',ShipZip='$row2[ShipZip]',ShipPhone='$row2[ShipPhone]', "
                . "BillName='$row2[BillName]',BillAddress='$row2[BillAddress]',BillAddress2='$row2[BillAddress2]',BillCity='$row2[BillCity]',BillState='$row2[BillState]', "
                . "BillZip='$row2[BillZip]',BillPhone='$row2[BillPhone]',Date='$row2[Date]',PaymentMethod='$row2[PaymentMethod]',CardNumber='$row2[CardNumber]', "
                . "Shipped='$row2[Shipped]',TrackingNumber='$row2[TrackingNumber]',User_id='$row2[User_id]' "
                . "WHERE id=$row2[id] ";
        //echo $sql;
        $conn->query($sql);
        $error = $conn->error;
        $conn->close();
                
        return $error != '' ? array('Server Error' => $error) : true;           
	}
	
	static function Delete($row)
	{
		$conn = GetConnection();
		$sql = "DELETE FROM 2012Grad_Orders "
				. "WHERE id=$row[id] ";
		$conn->query($sql);
		$error = $conn->error;
		
		return $error != '' ? array('Server Error' => $error) : true;
	}
	
	static function Validate($row)
	{
		$results = array();
		if(!is_numeric($row['Total'])) $results['Total'] = 'Total needs to be a number';
		if(empty($row['Total'])) $results['Total'] = 'Total is required';
		if(!is_numeric($row['Tax'])) $results['Tax'] = 'Tax needs to be a number';
		if(empty($row['Tax'])) $results['Tax'] = 'Tax is required';
		if(!is_numeric($row['Shipping'])) $results['Shipping'] = 'Shipping needs to be a number';
		if(empty($row['Shipping'])) $results['Shipping'] = 'Shipping is required';
		
		if(empty($row['ShipName'])) $results['ShipName'] = 'ShipName is required';
		if(empty($row['ShipAddress'])) $results['ShipAddress'] = 'ShipAddress is required';
		if(empty($row['ShipCity'])) $results['ShipCity'] = 'ShipCity is required';
		if(empty($row['ShipState'])) $results['ShipState'] = 'ShipState is required';
		if(!is_numeric($row['ShipZip'])) $results['ShipZip'] = 'ShipZip needs to be a number';
		if(empty($row['ShipZip'])) $results['ShipZip'] = 'ShipZip is required';
		if(empty($row['ShipPhone'])) $results['ShipPhone'] = 'ShipPhone is required';
		
		
		if(empty($row['BillName'])) $results['BillName'] = 'BillName is required';
		if(empty($row['BillAddress'])) $results['BillAddress'] = 'BillAddress is required';
		if(empty($row['BillCity'])) $results['BillCity'] = 'BillCity is required';
		if(empty($row['BillState'])) $results['BillState'] = 'BillState is required';
		if(!is_numeric($row['BillZip'])) $results['BillZip'] = 'BillZip needs to be a number';
		if(empty($row['BillZip'])) $results['BillZip'] = 'BillZip is required';
		if(empty($row['BillPhone'])) $results['BillPhone'] = 'BillPhoneis required';
		
		if(empty($row['Date'])) $results['Date'] = 'Date id is required';
		if(empty($row['PaymentMethod'])) $results['PaymentMethod'] = 'PaymentMethod is required';
		if(empty($row['User_id'])) $results['User_id'] = 'User id is required';
		
		return count($results) > 0 ? $results : true;
	}
	
}